<html>
<head>
<title>CpcPaChi - Version 0.02</title>
<style type="text/css">
span {
	font-family: 'Courier New';
	font-size: 10pt;
	color: #000000;
}
.sc0 {
	color: #808080;
}
.sc3 {
	color: #008080;
}
.sc4 {
	color: #FF8000;
}
.sc5 {
	font-weight: bold;
	color: #0000FF;
}
.sc7 {
	color: #808080;
}
.sc10 {
	font-weight: bold;
	color: #000080;
}
.sc11 {
}
</style>
</head>
<body>
<h2>Un compilador sencillo paso a paso (2)</h2>

<p align="right">(Volver al <a href="doc.html">&iacute;ndice</a>) </p>

<p>Este segundo acercamiento va a reconocer dos &oacute;rdenes m&aacute;s: WRITECHAR, que escribir&aacute; una &uacute;nica letra en pantalla, y LOCATE, que mover&aacute; el cursor a otras coordenadas de pantalla. Adem&aacute;s, en vez de aceptar una &uacute;nica orden por teclado, leer&aacute; varias desde disco.</p>

<p>As&iacute;, aceptar&aacute; programas como &eacute;ste:</p>
<pre>
cls
locate(10,5)
writeChar('a')
locate(1,10)
writeChar('b')
</pre>


<p>El hecho de que el usuario indique el nombre del fichero no es mucha complicaci&oacute;n: vemos con "paramcount" si realmente lo ha detallado, avisamos si no lo ha hecho, y leemos el nombre con "paramstr(1)" en caso contrario:</p>
<pre>
if paramcount >= 1 then
  nombreOrigen := paramstr(1)
else
  begin
  writeln('No se ha indicado el nombre del fichero');
  halt;
  end;
assign( ficheroOrigen, nombreOrigen );
reset( ficheroOrigen );
</pre>


<p>En cuanto a la traducci&oacute;n de writeChar('a'), se convertir&iacute;a en dos &oacute;rdenes de ensamblador: LD A, &amp;61; CALL &amp;BB5A. El primer problema es que tenemos que crear un c&oacute;digo que no ser&aacute; siempre el mismo, sino que depender&aacute; del par&aacute;metro que indiquemos entre comillas. Nuestra aproximaci&oacute;n por ahora ser&aacute; "poco pr&aacute;ctica":tomar la letra que aparece en la posici&oacute;n 12, y usar su c&oacute;digo ASCII en hexadecimal:</p>

<pre>
letra := orden[12];
writeln( ficheroDestino, lineaActual,' DATA 3E,',
  hexStr(ord(letra),2), ': '' LD A, "',letra, '"' );
writeln( ficheroDestino, lineaActual,
  ' DATA CD,5A,BB: '' CALL &BB5A - WRITECHAR' );
</pre>

<p>Adem&aacute;s, para cada nueva l&iacute;nea que generemos, aumentaremos el n&uacute;mero de l&iacute;nea del cargador en Basic y la cantidad de bytes de datos que hemos exportado:</p>
<pre>
lineaActual := lineaActual + 10;
longitudTotal := longitudTotal + 5;
</pre>

<p>Claramente, esta forma de saber la letra no es buena. &iquest;Qu&eacute; ocurre si hay un espacio antes de las comillas? La letra que debemos escribir ya no estar&iacute;a en la posici&oacute;n 12. Lo mismo ocurrir&aacute; si hay alg&uacute;n espacio antes de WRITECHAR o del par&eacute;ntesis. Por eso, en la pr&oacute;xima aproximaci&oacute;n crearemos un primer analizador l&eacute;xico, que sea capaz de extraer correctamente una palabra, aunque tenga espacios delante.</p>

<p>Por lo que respecta a la traducci&oacute;n de LOCATE, tenemos otro problema: la posici&oacute;n puede ser un n&uacute;mero de una cifra o de dos. Hasta que creemos un analizador l&eacute;xico que nos ayude, de momento volveremos a hacerlo de forma artesanal: leeremos una primera cifra, miraremos si la siguiente es la coma o una cifra num&eacute;rica, en caso de que sea num&eacute;rica deberemos multiplicar por 10 la cifra anterior y sum&aacute;rsela... muy trabajoso, pero es lo que haremos por ahora; pronto lo mejoraremos.</p>

<p>En un CPC hay dos formas de situar el cursor: LD A, columna; CALL &amp;BB6F y luego LD A, fila; CALL &amp;BB72, o bien indicar a la vez la fila y la columna el registro HL (H=columna, L=fila) y hacer CALL &amp;BB75. De momento usaremos la primera, que ser&aacute; algo m&aacute;s legible, aunque ocupe un poco m&aacute;s de espacio.</p>

<p>Con estas consideraciones, la parte que genera el c&oacute;digo para la primera parte de LOCATE, la columna, todav&iacute;a de forma muy rudimentaria y trabajosa, ser&aacute;:</p>

<pre>
posicion := 8;
x := ord(orden[posicion])-ord('0');
posicion := posicion + 1;
if orden[posicion] <> ',' then
  begin
  x := x*10 + ord(orden[posicion])-ord('0');
  posicion := posicion + 1;
  end;
writeln( ficheroDestino, lineaActual,' DATA 3E,',
  hexStr(x,2), ': '' LD A, ',x );
lineaActual := lineaActual + 10;
writeln( ficheroDestino, lineaActual,' DATA CD,6F,BB: '' CALL &BB6F - CURSOR COLUMN' );
lineaActual := lineaActual + 10;
</pre>

<p><br />As&iacute;, el c&oacute;digo generado para el programita anterior ser&iacute;a:</p>

<pre>
10 DATA CD,6C,BB: ' CALL &amp;BB6C - CLS
20 DATA 3E,0A: ' LD A, 10
30 DATA CD,6F,BB: ' CALL &amp;BB6F - CURSOR COLUMN
40 DATA 3E,05: ' LD A, 5
50 DATA CD,72,BB: ' CALL &amp;BB72 - CURSOR ROW
60 DATA 3E,61: ' LD A, "a"
70 DATA CD,5A,BB: ' CALL &amp;BB5A - WRITECHAR
80 DATA 3E,01: ' LD A, 1
90 DATA CD,6F,BB: ' CALL &amp;BB6F - CURSOR COLUMN
100 DATA 3E,0A: ' LD A, 10
110 DATA CD,72,BB: ' CALL &amp;BB72 - CURSOR ROW
120 DATA 3E,62: ' LD A, "b"
130 DATA CD,5A,BB: ' CALL &amp;BB5A - WRITECHAR
140 DATA C9: ' RET
150 longitud = 33
160 MEMORY 39999
170 FOR n=40000 TO 40000+longitud
180 READ a$:POKE n,VAL("&amp;"+a$)
190 NEXT
200 CALL 40000
</pre>


<p><br /><br />Y el conjunto de todo el fuente del compilador quedar&iacute;a as&iacute;:</p>

<pre>
<span class="sc5">program</span><span class="sc0"> </span><span class="sc11">cpcPaChi</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc3">(* Un compilador de Pascal chiquitito para CPC
   Por Nacho Cabanes - Version 0.02

   Versiones hasta la fecha:

   Num.   Fecha       Cambios
   ---------------------------------------------------
   0.02  22-Mar-2008  Admite CLS, LOCATE, WRITECHAR,
                      que lee desde fichero. Solo permite
                      una orden en cada linea, con formato fijo

   0.01  21-Mar-2008  Preliminar: solo acepta CLS por teclado
                      y genera el codigo correspondiente

*)</span><span class="sc0">


</span><span class="sc5">const</span><span class="sc0">
  </span><span class="sc11">lineaActual</span><span class="sc10">:</span><span class="sc0"> </span><span class="sc11">integer</span><span class="sc0"> </span><span class="sc10">=</span><span class="sc0"> </span><span class="sc4">10</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc11">longitudTotal</span><span class="sc10">:</span><span class="sc0"> </span><span class="sc11">integer</span><span class="sc0"> </span><span class="sc10">=</span><span class="sc0"> </span><span class="sc4">0</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc5">var</span><span class="sc0">
  </span><span class="sc11">ficheroOrigen</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">:</span><span class="sc0"> </span><span class="sc11">text</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc11">nombreOrigen</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">nombreDestino</span><span class="sc10">:</span><span class="sc0"> </span><span class="sc5">string</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc11">orden</span><span class="sc10">:</span><span class="sc0"> </span><span class="sc5">string</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc3">(* Analiza las opciones que teclee el usuario. Por ahora,
solo el nombre del fichero a compilar *)</span><span class="sc0">
</span><span class="sc5">procedure</span><span class="sc0"> </span><span class="sc11">analizarOpciones</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">begin</span><span class="sc0">
  </span><span class="sc5">if</span><span class="sc0"> </span><span class="sc11">paramcount</span><span class="sc0"> </span><span class="sc10">&gt;=</span><span class="sc0"> </span><span class="sc4">1</span><span class="sc0"> </span><span class="sc5">then</span><span class="sc0">
    </span><span class="sc11">nombreOrigen</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">paramstr</span><span class="sc10">(</span><span class="sc4">1</span><span class="sc10">)</span><span class="sc0">
  </span><span class="sc5">else</span><span class="sc0">
    </span><span class="sc5">begin</span><span class="sc0">
    </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc7">'No se ha indicado el nombre del fichero'</span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">halt</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc11">assign</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroOrigen</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">nombreOrigen</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">reset</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroOrigen</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">nombreDestino</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc7">'salida.bas'</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc11">assign</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">nombreDestino</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">rewrite</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
</span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc3">(* Obtiene una orden del programa fuente. La toma del fichero de origen *)</span><span class="sc0">
</span><span class="sc5">procedure</span><span class="sc0"> </span><span class="sc11">obtenerOrden</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">begin</span><span class="sc0">
  </span><span class="sc11">readln</span><span class="sc10">(</span><span class="sc11">ficheroOrigen</span><span class="sc10">,</span><span class="sc11">orden</span><span class="sc10">);</span><span class="sc0">
</span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">


</span><span class="sc3">(* Analiza la orden que el usuario ha dado, y sale con un mensaje
de error si es incorrecta *)</span><span class="sc0">
</span><span class="sc5">procedure</span><span class="sc0"> </span><span class="sc11">analizarUnaOrden</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">begin</span><span class="sc0">
  </span><span class="sc5">if</span><span class="sc0"> </span><span class="sc10">(</span><span class="sc11">upcase</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">)</span><span class="sc0"> </span><span class="sc10">&lt;&gt;</span><span class="sc0"> </span><span class="sc7">'CLS'</span><span class="sc10">)</span><span class="sc0">
      </span><span class="sc5">and</span><span class="sc0"> </span><span class="sc10">(</span><span class="sc11">pos</span><span class="sc10">(</span><span class="sc7">'LOCATE'</span><span class="sc10">,</span><span class="sc11">upcase</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">))&lt;&gt;</span><span class="sc4">1</span><span class="sc10">)</span><span class="sc0">
      </span><span class="sc5">and</span><span class="sc0"> </span><span class="sc10">(</span><span class="sc11">pos</span><span class="sc10">(</span><span class="sc7">'WRITECHAR'</span><span class="sc10">,</span><span class="sc11">upcase</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">))&lt;&gt;</span><span class="sc4">1</span><span class="sc10">)</span><span class="sc0">
      </span><span class="sc5">and</span><span class="sc0"> </span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">&lt;&gt;</span><span class="sc7">''</span><span class="sc10">)</span><span class="sc0"> </span><span class="sc5">then</span><span class="sc0">
    </span><span class="sc5">begin</span><span class="sc0">
    </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc7">'Error: orden no reconocida. Se esperaba: CLS, LOCATE o WRITECHAR'</span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">halt</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">


</span><span class="sc3">(* Genera el codigo de destino: codigo maquina de Amstrad CPC
dentro de un cargador en Basic *)</span><span class="sc0">
</span><span class="sc5">procedure</span><span class="sc0"> </span><span class="sc11">generarCodigoUnaOrden</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">var</span><span class="sc0">
  </span><span class="sc11">x</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">y</span><span class="sc10">:</span><span class="sc0"> </span><span class="sc11">byte</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc11">posicion</span><span class="sc10">:</span><span class="sc0"> </span><span class="sc11">byte</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc11">letra</span><span class="sc10">:</span><span class="sc0"> </span><span class="sc11">char</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">begin</span><span class="sc0">
  </span><span class="sc5">if</span><span class="sc0"> </span><span class="sc11">upcase</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">)</span><span class="sc0"> </span><span class="sc10">=</span><span class="sc0"> </span><span class="sc7">'CLS'</span><span class="sc0"> </span><span class="sc5">then</span><span class="sc0">
    </span><span class="sc5">begin</span><span class="sc0">
    </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">,</span><span class="sc7">' DATA CD,6C,BB: '' CALL &amp;BB6C - CLS'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">10</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">longitudTotal</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">longitudTotal</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">3</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc5">if</span><span class="sc0"> </span><span class="sc11">pos</span><span class="sc10">(</span><span class="sc7">'WRITECHAR'</span><span class="sc10">,</span><span class="sc11">upcase</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">))=</span><span class="sc4">1</span><span class="sc0"> </span><span class="sc5">then</span><span class="sc0">
    </span><span class="sc5">begin</span><span class="sc0">
    </span><span class="sc11">letra</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">orden</span><span class="sc10">[</span><span class="sc4">12</span><span class="sc10">];</span><span class="sc0">
    </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">,</span><span class="sc7">' DATA 3E,'</span><span class="sc10">,</span><span class="sc0">
      </span><span class="sc11">hexStr</span><span class="sc10">(</span><span class="sc11">ord</span><span class="sc10">(</span><span class="sc11">letra</span><span class="sc10">),</span><span class="sc4">2</span><span class="sc10">),</span><span class="sc0"> </span><span class="sc7">': '' LD A, "'</span><span class="sc10">,</span><span class="sc11">letra</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc7">'"'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">10</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">,</span><span class="sc7">' DATA CD,5A,BB: '' CALL &amp;BB5A - WRITECHAR'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">10</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">longitudTotal</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">longitudTotal</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">5</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc5">if</span><span class="sc0"> </span><span class="sc11">pos</span><span class="sc10">(</span><span class="sc7">'LOCATE'</span><span class="sc10">,</span><span class="sc11">upcase</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">))=</span><span class="sc4">1</span><span class="sc0"> </span><span class="sc5">then</span><span class="sc0">
    </span><span class="sc5">begin</span><span class="sc0">
    </span><span class="sc3">(* Leo X: 1 o 2 letras *)</span><span class="sc0">
    </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc4">8</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">x</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">ord</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">[</span><span class="sc11">posicion</span><span class="sc10">])-</span><span class="sc11">ord</span><span class="sc10">(</span><span class="sc7">'0'</span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">1</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc5">if</span><span class="sc0"> </span><span class="sc11">orden</span><span class="sc10">[</span><span class="sc11">posicion</span><span class="sc10">]</span><span class="sc0"> </span><span class="sc10">&lt;&gt;</span><span class="sc0"> </span><span class="sc7">','</span><span class="sc0"> </span><span class="sc5">then</span><span class="sc0">
      </span><span class="sc5">begin</span><span class="sc0">
      </span><span class="sc11">x</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">x</span><span class="sc10">*</span><span class="sc4">10</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc11">ord</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">[</span><span class="sc11">posicion</span><span class="sc10">])-</span><span class="sc11">ord</span><span class="sc10">(</span><span class="sc7">'0'</span><span class="sc10">);</span><span class="sc0">
      </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">1</span><span class="sc10">;</span><span class="sc0">
      </span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">,</span><span class="sc7">' DATA 3E,'</span><span class="sc10">,</span><span class="sc0">
      </span><span class="sc11">hexStr</span><span class="sc10">(</span><span class="sc11">x</span><span class="sc10">,</span><span class="sc4">2</span><span class="sc10">),</span><span class="sc0"> </span><span class="sc7">': '' LD A, '</span><span class="sc10">,</span><span class="sc11">x</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">10</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">,</span><span class="sc7">' DATA CD,6F,BB: '' CALL &amp;BB6F - CURSOR COLUMN'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">10</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc3">(* Tras la coma esta Y: 1 o 2 letras *)</span><span class="sc0">
    </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">1</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">y</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">ord</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">[</span><span class="sc11">posicion</span><span class="sc10">])-</span><span class="sc11">ord</span><span class="sc10">(</span><span class="sc7">'0'</span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">1</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc5">if</span><span class="sc0"> </span><span class="sc11">orden</span><span class="sc10">[</span><span class="sc11">posicion</span><span class="sc10">]</span><span class="sc0"> </span><span class="sc10">&lt;&gt;</span><span class="sc0"> </span><span class="sc7">')'</span><span class="sc0"> </span><span class="sc5">then</span><span class="sc0">
      </span><span class="sc5">begin</span><span class="sc0">
      </span><span class="sc11">y</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">y</span><span class="sc10">*</span><span class="sc4">10</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc11">ord</span><span class="sc10">(</span><span class="sc11">orden</span><span class="sc10">[</span><span class="sc11">posicion</span><span class="sc10">])-</span><span class="sc11">ord</span><span class="sc10">(</span><span class="sc7">'0'</span><span class="sc10">);</span><span class="sc0">
      </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">posicion</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">1</span><span class="sc10">;</span><span class="sc0">
      </span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">,</span><span class="sc7">' DATA 3E,'</span><span class="sc10">,</span><span class="sc0">
      </span><span class="sc11">hexStr</span><span class="sc10">(</span><span class="sc11">y</span><span class="sc10">,</span><span class="sc4">2</span><span class="sc10">),</span><span class="sc0"> </span><span class="sc7">': '' LD A, '</span><span class="sc10">,</span><span class="sc11">y</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">10</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">,</span><span class="sc7">' DATA CD,72,BB: '' CALL &amp;BB72 - CURSOR ROW'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
    </span><span class="sc3">(* 10 bytes *)</span><span class="sc0">
    </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">10</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">longitudTotal</span><span class="sc0"> </span><span class="sc10">:=</span><span class="sc0"> </span><span class="sc11">longitudTotal</span><span class="sc0"> </span><span class="sc10">+</span><span class="sc0"> </span><span class="sc4">10</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc3">(* Genera el codigo de destino final: cargador Basic *)</span><span class="sc0">
</span><span class="sc5">procedure</span><span class="sc0"> </span><span class="sc11">generarCodigoFinal</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">begin</span><span class="sc0">
  </span><span class="sc11">close</span><span class="sc10">(</span><span class="sc11">ficheroOrigen</span><span class="sc10">);</span><span class="sc0">

  </span><span class="sc11">append</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">,</span><span class="sc7">' DATA C9: '' RET'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">+</span><span class="sc4">10</span><span class="sc10">,</span><span class="sc7">' longitud = '</span><span class="sc10">,</span><span class="sc11">longitudTotal</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">+</span><span class="sc4">20</span><span class="sc10">,</span><span class="sc7">' MEMORY 39999'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">+</span><span class="sc4">30</span><span class="sc10">,</span><span class="sc7">' FOR n=40000 TO 40000+longitud'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">+</span><span class="sc4">40</span><span class="sc10">,</span><span class="sc7">' READ a$:POKE n,VAL("&amp;"+a$)'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">+</span><span class="sc4">50</span><span class="sc10">,</span><span class="sc7">' NEXT'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">writeln</span><span class="sc10">(</span><span class="sc0"> </span><span class="sc11">ficheroDestino</span><span class="sc10">,</span><span class="sc0"> </span><span class="sc11">lineaActual</span><span class="sc10">+</span><span class="sc4">60</span><span class="sc10">,</span><span class="sc7">' CALL 40000'</span><span class="sc0"> </span><span class="sc10">);</span><span class="sc0">
  </span><span class="sc11">close</span><span class="sc10">(</span><span class="sc11">ficheroDestino</span><span class="sc10">);</span><span class="sc0">
</span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">

</span><span class="sc3">(* Cuerpo del programa *)</span><span class="sc0">
</span><span class="sc5">begin</span><span class="sc0">
  </span><span class="sc11">analizarOpciones</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc5">while</span><span class="sc0"> </span><span class="sc5">not</span><span class="sc0"> </span><span class="sc11">eof</span><span class="sc10">(</span><span class="sc11">ficheroOrigen</span><span class="sc10">)</span><span class="sc0"> </span><span class="sc5">do</span><span class="sc0">
    </span><span class="sc5">begin</span><span class="sc0">
    </span><span class="sc11">obtenerOrden</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">analizarUnaOrden</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc11">generarCodigoUnaOrden</span><span class="sc10">;</span><span class="sc0">
    </span><span class="sc5">end</span><span class="sc10">;</span><span class="sc0">
  </span><span class="sc11">generarCodigoFinal</span><span class="sc10">;</span><span class="sc0">
</span><span class="sc5">end.</span><span class="sc0">
</span>
</pre>

<p align="right">(Volver al <a href="doc.html">&iacute;ndice</a>) </p>
</body>
</html>
